namespace UmSoft.SharePoint.Integration._1Cv77
{
    using System;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;
    using UmSoft.SharePoint.Integration._1Cv77;

    public class V7Object : IDisposable, IComObject
    {
        private object comObject;
        private IComObject application;
        protected V7Object(V7Object v7Object) : base()
        {
            this.comObject = v7Object.Object;
            this.application = v7Object.Application;
        }
        internal V7Object(object v7ComObject, IComObject application)
        {
            this.comObject = v7ComObject;
            this.application = application;
            (this.Application as V7).Activate(this.comObject);

        }
        protected T GetObject<T>(V7Object o)
        {
            if (o == null)
            {
                return default(T);
            }
            return (T)Activator.CreateInstance(typeof(T), new object[] { o });

        }
        public virtual void SetProperty(string propertyName, object value)
        {
            if (this.comObject != null)
            {
                object obj2 = value;
                if (value is V7Object)
                {
                    obj2 = ((V7Object)value).Object;
                }
                if (value == DBNull.Value)
                {
                    obj2 = null;
                }
                try
                {
                    this.comObject.GetType().InvokeMember(propertyName, BindingFlags.PutDispProperty, null, this.comObject, new object[] { obj2 });
                }
                catch (Exception exception)
                {
                    if (exception.InnerException != null)
                    {
                        Exception exception2 = new Exception(exception.InnerException.Message);
                        throw exception2;
                    }
                    throw exception;
                }
            }

        }
        public virtual object GetProperty(string propertyName)
        {
            object obj;
            Exception exception;
            object obj1;
            if (this.comObject == null)
            {
                return null;
            }
            try
            {
                obj = this.comObject.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, this.comObject, null);
                if (((obj != null) && (obj is string)) && ((Application as V7).TrimString))
                {
                    obj = (obj as string).Trim();
                }
                if ((obj != null) && (Marshal.IsComObject(obj)))
                {
                    return new V7Object(obj, Application);
                }
                obj1 = obj;
            }
            catch (Exception exception2)
            {
                exception = exception2;
                if (exception.InnerException != null)
                {
                    throw new Exception(exception.InnerException.Message);
                }
                throw exception;
            }
            return obj1;
        }
        public virtual object InvokeMethod(string methodName, params object[] args)
        {
            object obj4;
            if (this.comObject == null)
            {
                return null;
            }
            try
            {
                object[] objArray = null;
                if (args != null)
                {
                    objArray = new object[args.Length];
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (args[i] is V7Object)
                        {
                            objArray[i] = (args[i] as V7Object).Object;
                        }
                        else
                        {
                            objArray[i] = args[i];
                        }
                    }
                }
                object o = this.comObject.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, this.comObject, objArray);
                if (((o != null) && (o is string)) && (this.Application as V7).TrimString)
                {
                    o = (o as string).Trim();
                }
                if ((o != null) && Marshal.IsComObject(o))
                {
                    return new V7Object(o, this.Application);
                }
                obj4 = o;
            }
            catch (Exception exception)
            {
                if (exception.InnerException != null)
                {
                    Exception exception2 = new Exception(exception.InnerException.Message);
                    throw exception2;
                }
                throw exception;
            }
            return obj4;

        }
        public virtual void Dispose()
        {
            int i;
            (Application as V7).Deactivate(Object);
            if (this.comObject != null)
            {
                i = Marshal.ReleaseComObject(this.comObject);
                while (i > 0)
                {
                    i = Marshal.ReleaseComObject(this.comObject);
                }
                return;
            }
            else
            {
                return;
            }
        }
        public override string ToString()
        {
            return this.Application.InvokeMethod("ЗначениеВСтроку", new object[] { this }).ToString();

        }
        internal object Object
        {
            get
            {
                return this.comObject;
            }
        }
        protected IComObject Application
        {
            get
            {
                return this.application;

            }
            set
            {
                this.application = value;
            }
        }
        public bool Empty
        {
            get
            {
                return this.Application.InvokeMethod("ПустоеЗначение", new object[] { this.Object }).Equals(V7.TRUE);

            }
        }
        public AggregateType Type
        {
            get
            {
                AggregateType aggregateType;
                lock (new object())
                {
                    Application.SetProperty("Интеграция", this);
                    aggregateType = (AggregateType)Enum.ToObject(typeof(AggregateType), Convert.ToInt32((Application as V7).EvalExpr(string.Format("ТипЗначения({0})", "Интеграция"))));
                }
                return aggregateType;
            }
        }
        public string TypeAsString
        {
            get
            {
                lock (new object())
                {
                    this.Application.SetProperty("Интеграция", this);
                    return (this.Application as V7).EvalExpr(string.Format("ТипЗначенияСтр({0})", "Интеграция")).ToString();
                }

            }
        }
    }
}
